package com.db.protocol.tags;

import com.db.protocol.exception.IncorrectValueProtocolException;
import com.db.protocol.exception.NotParsingValueProtocolException;
import com.db.protocol.exception.ParseProtocolException;

public enum RejectReason {

	INVALID_TAG(0),
	TAG_MISSING(1),
	INCORRECT_VALUE(5),
	NOT_PARSING_VALUE(6),
	INVALID_MSG(8),
	FAILED_ID(9);
	
	private final int reason;
	
	public int value() {
		return reason;
	}
	
	RejectReason(int reason) {
		this.reason = reason;
	}
	
	public static Tag tag() {
		return Tag.REJECT_REASON;
	}

	public static RejectReason parse(String message) throws ParseProtocolException {
		if (message == null) {
			throw new NotParsingValueProtocolException(tag());
		}
		try {
			int value = Integer.valueOf(message);
		
			for (RejectReason type : values()) {
				if (type.value() == value) {
					return type;
				}
			}
		} catch (NumberFormatException exception) {
			throw new NotParsingValueProtocolException(tag());
		}
		throw new IncorrectValueProtocolException(tag());	
	}
	
	@Override
	public String toString() {
		switch (this) {
			case INCORRECT_VALUE:
				return "Incorrect value";
			case FAILED_ID:
				return "Failed value";
			case INVALID_MSG:
				return "Invalid message";
			case INVALID_TAG:
				return "Invalid tag";
			case NOT_PARSING_VALUE:
				return "Not parsing value";
			case TAG_MISSING:
				return "Tag missing";
		}
		return super.toString();
	}
	
}
